package com.maxima.attendance.reports;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;

@SuppressWarnings("deprecation")
public class CreateReport {

	public File generateReport(String projectName,
			Map<Integer, Object[]> data) {

		HSSFWorkbook workBook = new HSSFWorkbook();

		HSSFSheet sheet = workBook.createSheet("Employee Attendance");

		HSSFFont font = workBook.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

		HSSFCellStyle boldStyle = workBook.createCellStyle();
		boldStyle.setFont(font);

		HSSFCellStyle normalCellStyle = workBook.createCellStyle();
		normalCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		HSSFCellStyle headerStyle = workBook.createCellStyle();
		headerStyle.setFont(font);
		headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
				.getIndex());
		headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		HSSFCellStyle holidayStyle = workBook.createCellStyle();
		// holidayStyle.setFont(font);
		holidayStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		holidayStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW
				.getIndex());
		holidayStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		
		HSSFCellStyle weeklyOffStyle = workBook.createCellStyle();
		// holidayStyle.setFont(font);
		weeklyOffStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		weeklyOffStyle.setFillForegroundColor(IndexedColors.SKY_BLUE
				.getIndex());
		weeklyOffStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		HSSFCellStyle wfhStyle = workBook.createCellStyle();
		// holidayStyle.setFont(font);
		wfhStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		wfhStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
		wfhStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		HSSFCellStyle leaveStyle = workBook.createCellStyle();
		// holidayStyle.setFont(font);
		leaveStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		leaveStyle
				.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
		leaveStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		// Iterate over data and write to sheet
		Set<Integer> keyset = data.keySet();
		int rownum = 0;
		for (Integer key : keyset) {
			Row row = sheet.createRow(rownum);

			Object[] objArr = data.get(key);

			int cellnum = 0;
			if (objArr.length == 3) {
				for (Object obj : objArr) {
					Cell cell = row.createCell(cellnum++);
					if (obj instanceof String)
						cell.setCellValue((String) obj);
					else if (obj instanceof Integer)
						cell.setCellValue((Integer) obj);
					cell.setCellStyle(normalCellStyle);
				}
				sheet.addMergedRegion(new CellRangeAddress(rownum, rownum,
						cellnum - 1, cellnum + 1));
				
				if (objArr[objArr.length - 1].toString().equals("Holiday")){
					row.getCell(cellnum - 1).setCellStyle(holidayStyle);
				}
				else if (objArr[objArr.length - 1].toString().equals("Weekly Off")){
					row.getCell(cellnum - 1).setCellStyle(weeklyOffStyle);
				}
				else if (objArr[objArr.length - 1].toString().equals("Leave")){
					row.getCell(cellnum - 1).setCellStyle(leaveStyle);
				}
				else {
					row.getCell(cellnum - 1).setCellStyle(wfhStyle);
				}
			} else if (objArr.length == 0) {
				rownum++;
			} else {
				for (Object obj : objArr) {

					Cell cell = row.createCell(cellnum++);
					if (obj instanceof String)
						cell.setCellValue((String) obj);
					else if (obj instanceof Integer)
						cell.setCellValue((Integer) obj);
					cell.setCellStyle(normalCellStyle);
					if (rownum == 0)
						cell.setCellStyle(headerStyle);
				}
			}
			rownum++;
		}
		for (int columnIndex = 0; columnIndex < 10; columnIndex++) {
			sheet.autoSizeColumn(columnIndex);
		}
		try {
			File f= new File(projectName + "_" + "report.xls");
			FileOutputStream out = new FileOutputStream(f);
			workBook.write(out);
			out.close();
			return f;
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return null;
	}

}
